package com.example.hotel_lxy.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.bean.ZeroSpSourceBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.DisplayUtils;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

//客户来源  客户经理
public class DataSelectionDialogs extends Dialog {
    private Context mContext;
    RecyclerView rv_Data_selection;
    private static  String Url;
    private String id_type;
    private String name;
    List<InfoBean> list;
    public DataSelectionDialogs(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    private View init() {
        View view = View.inflate(mContext, R.layout.dialog_dataselection, null);
        rv_Data_selection=view.findViewById(R.id.rv_Data_selection);
        showData();
        return view;
    }

    private void showData() {
        OkHttpUtils.post()
                .url(Url)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "客户来源onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "客户来源onResponse   " + response);
                        ZeroSpSourceBean zeroSpSourceBean = JsonUtils.parseByGson(response, ZeroSpSourceBean.class);
                        if (zeroSpSourceBean.getCode().equals("0")) {
                            list=new ArrayList<>();
                            list.clear();
                            for (int i = 0; i <zeroSpSourceBean.getData().getList().size() ; i++) {
                                ZeroSpSourceBean.DataBean.ListBean listBean = zeroSpSourceBean.getData().getList().get(i);
                                InfoBean infoBean1=new InfoBean();
                                infoBean1.setId(listBean.getId());
                                infoBean1.setName(listBean.getName());
                                infoBean1.setUsername(listBean.getUsername());
                                list.add(infoBean1);
                            }
                            rv_Data_selection.setLayoutManager(new LinearLayoutManager(mContext));
                            RBaseAdapter<InfoBean> rBaseAdapterAllType = new RBaseAdapter<InfoBean>(
                                    mContext, R.layout.item_item_popwindow, DataSelectionDialogs.this.list) {
                                @Override
                                protected void fillItem(RViewHolder holder, InfoBean item, int position) {
                                    TextView text_title = holder.itemView.findViewById(R.id.text_title);
                                    if(item.getName()!=null){
                                        text_title.setText(item.getName());
                                    }else{
                                        text_title.setText(item.getUsername());
                                    }

                                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            id_type = item.getId() + "";
                                            if(item.getName()!=null){
                                                name=item.getName();
                                            }else{
                                                name=item.getUsername();
                                            }
                                            listener.onPositiveClick(id_type,name);
                                            dismiss();
                                        }
                                    });
                                }

                                @Override
                                protected int getViewType(InfoBean item, int position) {
                                    return 0;
                                }
                            };
                            rv_Data_selection.setAdapter(rBaseAdapterAllType);
                        }

                    }
                });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
//        setCancelable(false);
        setContentView(init());
    }

    public void showDialog() {
        if (!isShowing()) {
            show();
            WindowManager.LayoutParams lp = getWindow().getAttributes();
//            lp.height = DisplayUtils.getDisplayWidth(mContext) * 5 / 5;
            lp.width = DisplayUtils.getDisplayWidth(mContext) * 8 / 10;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(lp);
            getWindow().setGravity(Gravity.CENTER);
        }
    }

    public static DataSelectionDialogs show(Context context, OnDialogClickListener listener, String url) {
        DataSelectionDialogs dialog = new DataSelectionDialogs(context, R.style.BottomDialogStyle);
        dialog.setListener(listener);
        Url=url;
        dialog.showDialog();
        return dialog;
    }

    public OnDialogClickListener listener;

    public DataSelectionDialogs setListener(OnDialogClickListener listener) {
        this.listener = listener;
        return this;
    }

    public interface OnDialogClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(String id, String name);
    }
    public class InfoBean{
        private int id;
        private String name;
        private String username;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
